/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var threeSum = function(nums) {
  if(nums.length < 3)
    return [];
  // 1.排序
  let arr = nums.sort(function(a, b) {
    if(a > b) // 如果 a 大于 b,位置互换
        return 1;
    else //否则，位置不变
        return -1;
  });
  function checkRepeat(i, j, k) {
    for(let l = 0; l < result.length; l++) {
      if(result[l][0] === nums[i] && result[l][1] === nums[j] && result[l][2] === nums[k])
        return true;
    }
    return false;
  }
  let result = [];
  for(let i = 0; i < nums.length - 2; i++) {
    let j = i + 1, k = nums.length - 1;
    while(j < k) {
      // console.log(i, j, k);
      if(nums[i] + nums[j] + nums[k] === 0) {
        if(result.length) {
          if(checkRepeat(i, j, k))
            {
              j++, k--;
              continue;
            }
        }
        result.push([nums[i], nums[j], nums[k]]);
        j++, k--;
      } else {
        if(nums[i] + nums[j] + nums[k] < 0) {
          j++;
        } else {
          k--;
        }
      }
    }
  }
  return result;
};
let nums = [-1,0,1,2,-1,-4];
console.log(threeSum(nums));
nums = [];
console.log(threeSum(nums));
nums = [0];
console.log(threeSum(nums));
nums = [-1,0,1,0];
console.log(threeSum(nums));
nums = [0,0,0];
console.log(threeSum(nums));
nums = [-4,-2,-2,-2,0,1,2,2,2,3,3,4,4,6,6];
console.log(threeSum(nums));
